const RECORD_KEY = 'workout_records'

export const workoutRecordService = {
  getRecords() {
    try {
      const records = localStorage.getItem(RECORD_KEY)
      return records ? JSON.parse(records) : []
    } catch (error) {
      console.error('获取记录失败:', error)
      return []
    }
  },

  addRecord(record) {
    try {
      const records = this.getRecords()
      const newRecord = {
        ...record,
        id: Date.now().toString(),
        date: new Date().toISOString()
      }
      records.push(newRecord)
      localStorage.setItem(RECORD_KEY, JSON.stringify(records))
      return newRecord
    } catch (error) {
      console.error('保存记录失败:', error)
      throw error
    }
  }
}